package web.backstage.product;

import bean.ProductBean;
import org.apache.commons.lang.StringUtils;
import pojo.Category;
import pojo.Product;
import service.CategoryService;
import service.ProductService;
import service.impl.CategoryServiceImpl;
import service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 查询商品信息
 * @author wyx
 * @version 1.0
 * @date 2020/11/20
 */
@WebServlet("/FindProductServlet")
public class FindProductServlet extends HttpServlet {

    ProductService productService = new ProductServiceImpl();
    CategoryService categoryService = new CategoryServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        String id = request.getParameter("id");
        String category = request.getParameter("category");
        String name = request.getParameter("name");
        String minPrice = request.getParameter("minPrice");
        String maxPrice = request.getParameter("maxPrice");


        ProductBean pb = new ProductBean(id,name,category,minPrice,maxPrice);

        List<Product> products = productService.findProduct(pb);
        request.setAttribute("allProduct", products);

        List<Category> categorys = categoryService.queryAllCategory();
        request.setAttribute("allCategory", categorys);

        if (StringUtils.isNotEmpty(category)) {
            request.setAttribute("categoryId",category);
        }
        if (StringUtils.isNotEmpty(id)) {
            request.setAttribute("Id",id);
        }
        if (StringUtils.isNotEmpty(name)) {
            request.setAttribute("categoryName",name);
        }
        if (StringUtils.isNotEmpty(minPrice)) {
            request.setAttribute("minPrice",minPrice);
        }
        if (StringUtils.isNotEmpty(maxPrice)) {
            request.setAttribute("maxPrice",maxPrice);
        }
        request.getRequestDispatcher("WEB-INF/views/product/productList.jsp").forward(request, response);
    }

}
